<template>
  <div>
    <!-- 导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区 -->
    <el-card>
      <!-- 搜索 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            @clear="getOrderList"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 列表 -->
      <el-table :data="orderList" border stripe style="width: 100%">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="100"
        ></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="100"
        ></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="185">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <!-- {{ scope.row }} -->
          <!-- 修改地址 -->
          <el-tooltip content="修改地址" placement="top" :enterable="false">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showBox"
            ></el-button>
          </el-tooltip>
          <!-- 物流进度 -->
          <el-tooltip content="物流进度" placement="top" :enterable="false">
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location-outline"
              @click="showProgressBox"
            ></el-button>
          </el-tooltip>
        </el-table-column>
      </el-table>

      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址 -->
    <el-dialog
      title="修改地址"
      :visible.sync="etidAddressDialogVisible"
      width="50%"
      @close="etidAddressClosed"
    >
      <el-form
        :model="etidAddressForm"
        :rules="etidAddressFormRules"
        ref="etidAddressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityDate"
            v-model="etidAddressForm.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="etidAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="etidAddressDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="etidAddress">确认</el-button>
      </div>
    </el-dialog>

    <!-- 物流进度 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(progress, index) in progressForm"
          :key="index"
          :timestamp="progress.time"
        >
          {{ progress.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityDate from "./city_data2017_element.js";

export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 20,
      },
      orderList: [],
      total: 0,
      etidAddressDialogVisible: false,
      etidAddressForm: {
        address1: [],
        address2: "",
      },
      cityDate,
      // 修改表单验证
      etidAddressFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" },
        ],
        address2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      // 物流
      progressVisible: false,
      progressForm: [],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo,
      });
      if (res.meta.status != 200)
        return this.$message.error("获取订单信息失败！");
      this.orderList = res.data.goods;
      this.total = res.data.total;
      // console.log(res);
    },
    // 页码
    handleSizeChange(newSize) {
      //   console.log(`每页 ${newSize} 条`);
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPage) {
      //   console.log(`当前页: ${newPage}`);
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    // 修改地址页面的关闭事件,关闭后重置页面数据
    etidAddressClosed() {
      this.$refs.etidAddressFormRef.resetFields();
    },
    // 修改地址显示验证
    async showBox() {
      this.etidAddressDialogVisible = true;
    },
    // 修改地址确认
    etidAddress() {},
    // 物流进度显示验证
    async showProgressBox() {
      const { data: res } = await this.$http.get("/kuaidi/1106975712662");
      if (res.meta.status != 200)
        return this.$message.error("获取物流信息失败！");
      this.progressForm = res.data;
      console.log(res);
      this.progressVisible = true;
    },
  },
};
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>